package com.ctgu.tengyun.controller.product;

import com.ctgu.tengyun.bean.convert.FileConvert;
import com.ctgu.tengyun.bean.convert.ProductRepairRecordConvert;
import com.ctgu.tengyun.bean.request.ProductRepairQuery;
import com.ctgu.tengyun.bean.response.ProductRepairListInfoResponse;
import com.ctgu.tengyun.domain.BooleanStatus;
import com.ctgu.tengyun.domain.ProductInfo;
import com.ctgu.tengyun.domain.ProductRepairRecord;
import com.ctgu.tengyun.domain.RepairType;
import com.ctgu.tengyun.service.product.ProductInfoService;
import com.ctgu.tengyun.service.product.ProductRepairRecordService;
import com.ctgu.tengyun.util.ParameterTool;
import com.ctgu.tengyun.util.StringUtils;
import com.ctgu.tengyun.util.Validator;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/12.
 */
@RestController
@RequestMapping(value = "/v1/product_repair/")
public class ProductRepairRecordController {

    @Resource
    private ProductRepairRecordService productRepairRecordService;
    @Resource
    private ProductInfoService productInfoService;

    @RequestMapping(value = "save_or_update", method = RequestMethod.POST)
    public Object save(@RequestBody ProductRepairRecord request) {

        Validator.assertHasFields(!StringUtils.isEmpty(request.getProductNo()), "productNo is required.");
        Validator.assertHasFields(null != request.getRepairDate(), "repairDate is required.");
        Validator.assertHasFields(null != request.getRepairType(), "repairType is required.");
        Validator.assertHasFields(null != request.getRepairFile(), "repairFile is required.");
        Validator.assertHasFields(null != request.getRecordPerson(), "recordPerson is required.");
        Validator.assertHasFields(null != request.getIsTrouble(), "isTrouble is required.");
        FileConvert.splitFileBeanUrlPath(request.getRepairFile());
        RepairType.valueOf(request.getRepairType());

        ProductInfo productInfo = productInfoService.selectByProductNo(request.getProductNo());
        Validator.assertResourceFound(null != productInfo, "product not found, productNo:" + request.getProductNo());

        if (null != request.getId()) {
            ProductRepairRecord repairRecord = productRepairRecordService.selectByPrimaryKey(request.getId());
            Validator.assertResourceFound(null != repairRecord, "repairRecord not found, recordId:" + request.getId());

            productRepairRecordService.updateByPrimaryKeySelective(request);
        } else {
            productRepairRecordService.insertSelective(request);
        }

        return ImmutableMap.of("id", request.getId());
    }

    @GetMapping(value = "list_info")
    public ProductRepairListInfoResponse listInfo(HttpServletRequest request) {
        Integer pageNo = ParameterTool.getParameterInteger(request, "pageNo", 1);
        Integer pageSize = ParameterTool.getParameterInteger(request, "pageSize", 10);
        String productNo = ParameterTool.getParameterString(request, "productNo", "");
        Long repairDate = ParameterTool.getParameterLong(request, "repairDate", null);
        Integer repairType = ParameterTool.getParameterInteger(request, "repairType", null);
        Integer isTrouble = ParameterTool.getParameterInteger(request, "isTrouble", null);

        Validator.validateGt(pageNo, 0);
        Validator.validateBetweenIn(pageSize, 0, 1000);
        if (null != repairType) {
            RepairType.valueOf(repairType);
        }
        if (null != isTrouble) {
            BooleanStatus.valueOf(isTrouble);
        }

        ProductRepairQuery repairQuery = new ProductRepairQuery();
        repairQuery.setPageNo(pageNo);
        repairQuery.setPageSize(pageSize);
        repairQuery.setProductNo(productNo);
        repairQuery.setRepairDate(repairDate);
        repairQuery.setRepairType(repairType);
        repairQuery.setIsTrouble(isTrouble);

        Integer totalCount = productRepairRecordService.countProductRepairRecord(repairQuery);
        List<ProductRepairRecord> repairRecordList = Collections.emptyList();
        if (totalCount > 0) {
            repairRecordList = productRepairRecordService.selectProductRepairRecord(repairQuery);
        }

        return ProductRepairRecordConvert.toProductRepairListInfoResponse(totalCount, pageSize, repairRecordList);
    }

    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public ProductRepairRecord detail(@PathVariable("id") Long id) {
        ProductRepairRecord repairRecord = productRepairRecordService.selectByPrimaryKey(id);
        Validator.assertResourceFound(null != repairRecord, "repair record not found, repairId:" + id);

        return ProductRepairRecordConvert.fillRepairRecord(repairRecord);
    }

}
